local ActorManage = require("app.manage.ActorManage"):GetInstance()

local LayerBase = require("app.Base.LayerBase")

local MapLayer = class("MapLayer", LayerBase)
MapLayer.instance = nil

function MapLayer:ctor(data)
    print("MapLayer")
    self.SpritePos = {}
    self.SpriteTable = {}    
    self.MapTable = {}
    
    self.data = self.data or td.Map --地图设定 width = 展示宽度 , height = 展示高度 , num_x = 横向格子数 , num_y = 纵向格子数 , w = 格子宽度 , h = 格子高度
 
    self:initUI()

     --    self.GridPos = {}
    self:CreatGridPos()
    td.MapLayer = self
end

function MapLayer:GetInstance()
	if MapLayer.instance == nil then
		MapLayer.instance = MapLayer.new()
	end
	return MapLayer.instance
end

function MapLayer:initUI()
    self:CreateSpritePos(self.data.w,self.data.h,self.data.num_x,self.data.num_y)--单个长宽，长宽数量
    self:CreateSprite()

end

function MapLayer:CreateSpritePos(w,h,x,y)   
    self.pos = {x=0,y=0}
    for i=1,x do
        self.SpritePos[i] = {}
        for t=1,y do
            self.SpritePos[i][t] = { x = w*i-w*0.5 + self.pos.x,y = h*t-h*0.5 + self.pos.y ,scaleX = w/100,scaleY = h/100 ,w=w,h=h}
        end
    end
end

function MapLayer:CreateSprite()    
    for i, var in pairs(self.SpritePos) do
        if i%2==1 then 
            for t, v in pairs(var) do
                if t%2==1 then 
                    local pos = self.SpritePos[i][t]
                    local Sprite = display.newSprite("UI/tipe/2to2.png")
                    Sprite:scale(pos.scaleX,pos.scalseY)
                    Sprite:move(pos.x+pos.w*0.5,pos.y+pos.h*0.5)
                    Sprite:addTo(self)
    --            self.SpriteTable[i,t] 
                end
            end
        end        
    end    
end

function MapLayer:GetPosForGrid(Grid)    --根据网格（格点）获取坐标（格点中心位置坐标）
    return self.SpritePos[Grid.x][Grid.y]
end

function MapLayer:GetGridForPos(pos)    -- 根据当前坐标获取对应格点位置
    local width = self.data.w
    local height = self.data.h

--    local Grid = { }
    pos.gridX = math.ceil(pos.x / width)
    pos.gridY = math.ceil(pos.y / height)

    return pos
end

function MapLayer:GetActorByPos(pos)
    local actorList = {}
    if self.MapTable[pos.x] then 
        if self.MapTable[pos.x][pos.y] then 
            for i, var in ipairs(self.MapTable[pos.x][pos.y]) do
                local actor = ActorManage:GetActor(var)
                table.insert(actorList,actor)
            end 
        end
    end
    return actorList
end

function MapLayer:GetCoverArea(actor)    -- 获取演员
    --    local _GridPos = self.GridPos[Grid.x][Grid.y] --获取这一个格子的范围，然后判定八个方向是否超格
    --使用八次判定，判定八个方向是否超格，如果超格则继续往外判定，最后所有经历的格子记录下来视为该单位占据的格子部分
    local _pos = actor:pos()
    _pos = self:GetGridForPos(_pos)
    local GridList = td.SeekRoadFor4(_pos,actor._property.area_covered,self.GridPos)

    return GridList
end

function MapLayer:CreatGridPos()
    local range = { }
    local num_x = self.data.num_x
    local num_y = self.data.num_y
    local width = self.data.w
    local height = self.data.h
    for i=1,num_x do
        range[i] = { }
        for t=1,num_y do
            local x_min = width*(i-1)
            local x_max = width*i
            local y_min = height*(t-1)
            local y_max = height*t

            range[i][t] = { x_min = x_min , x_max = x_max , y_min = y_min , y_max = y_max , x_core = self.SpritePos[i][t].x , y_core = self.SpritePos[i][t].y }
        end
    end 
--    return range
    self.GridPos = range
    td.GridPos = range
end

function MapLayer:GetActorsForPos()
--    self:GetGridPos()
    local _ActorTable = {}
    local _MapTable = {}
    local ActorList = ActorManage:GetActor()
    for id, actor in pairs(ActorList) do
--        local Grid = self:GetGridForPos(actor:pos())
--        if _MapTable[Grid.x] == nil then _MapTable[Grid.x] = { } end
--        if _MapTable[Grid.x][Grid.y] == nil then _MapTable[Grid.x][Grid.y] = { } end
--        table.insert( _MapTable[Grid.x][Grid.y] , actor)

--        local Grid = self:GetCoverArea(actor)   --多点占据，如果一个单位足够大则会一次性触碰多个点
        local Grid --= {td.GetGridForPos(actor:pos())}
        
        if actor._target.pos[1] then Grid = {actor._target.pos[1]} end

        local _Actor = { actor = actor , grid = {} }
        for i, var in ipairs(Grid) do
            if _MapTable[var.gridX] == nil then _MapTable[var.gridX] = { } end
            if _MapTable[var.gridX][var.gridY] == nil then _MapTable[var.gridX][var.gridY] = { } end
            table.insert( _MapTable[var.gridX][var.gridY] , actor.id)
            table.insert( _Actor.grid , { x = var.gridX , y = var.gridY })
        end
        table.insert( _ActorTable , _Actor )
    end
    self.MapTable = _MapTable
    self.ActorTable = _ActorTable
--    dump(self.MapTable)
--    for _, actor in ipairs(self.ActorTable) do
--        dump(actor.grid)
--    end
    
end

function MapLayer:GetActorTable()
    return self.ActorTable
end

function MapLayer:GetMapTable()
    return self.MapTable
end

function MapLayer:Update(dt)
    self:GetActorsForPos()
end

return MapLayer